<%
  game = Game.find(session[:player].game_id)
  game_setting = game.game_setting
  player = Player.find(session[:player].id)
  sales = Sale.find(:all, :conditions=>["game_id=? AND player_id=? AND cash>0", game.id, player.id])
%>
<a href="/user/plan_game">วางแผนการผลิตและการเงิน (B)</a> &nbsp;&nbsp;&nbsp;&nbsp;
<%if game.a_player>0%>
<a href="/user/rawmarket">ตลาดซื้อขาย <%=game_setting.inventory_a_name%></a> &nbsp;&nbsp;&nbsp;&nbsp;
<%end%>
<a href="/user/foundgame">ตลาดซื้อขาย <%=game_setting.inventory_b_name%></a>
<font color="red"><%=@word%></font><br/>
Game Session : <%=game.name%> &nbsp;&nbsp;&nbsp;&nbsp; Year: <%=game.current_year%> &nbsp;&nbsp;&nbsp;&nbsp; Round: <%=game.current_round%>
&nbsp;&nbsp;&nbsp;&nbsp;
<%if game.run_flag=='N'%>
<img src="/images/stop_icon.jpg" width="50px" style="vertical-align:middle;"/>
<%else%>
<img src="/images/start_icon.gif" width="50px" style="vertical-align:middle;"/>
<%end%>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="/user/foundgame"><img src="/images/refresh_icon.png" border="0" style="vertical-align:middle;" alt="Refresh" title="Refresh"/></a><br/>
<table width="100%">
  <tr>
    <td valign="top" width="40%">
      <form action="/user/save_custom" method="post">
      Join As : <%=player.name%> [<input type="text" name="custom_name" value="<%if !player.custom_name%>custom name<%else%><%=player.custom_name%><%end%>"/>] <input type="submit" value="Save"/>
      </form>
      <form action="/user/save_inventory" method="post">
      Inventory(Unit) : <%=number_with_delimiter(player.remain_inventory)%>
      </form>
      <form action="/user/save_price" method="post">
      Price(บาท) : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="price" value="<%=player.price%>"/> <input type="submit" value="Save"/>
      </form>
      <%if game_setting.promotion=='Y'%>
      <form action="/user/save_promotion" method="post">
      Promotion(ล้านบาท) :
        <input type="text" name="promotion" value="<%=number_with_precision(player.promotion/1000000.0, :precision=>2)%>"/>
        <input type="submit" value="Save"/>
        <br/><font color="red">* รองรับทศนิยม 2 ตำแหน่ง</font>
      </form>
      <%end%>
      Roll : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/user/roll_share"><img src="/images/dice_roll.gif" border="0" width="100px" style="vertical-align: middle;"/></a>
    </td>
    <td valign="top">
      <table>
        <tr>
          <td><span style="font-size:40px;">Market Size:</span></td>
          <td align="right"><span style="font-size:60px; color:blue;"><%=number_with_delimiter(game.market_size)%></span></td>
        </tr>
        <tr>
          <td><span style="font-size:40px;">Remains:</span></td>
          <td align="right"><span style="font-size:60px; color:red;"><%=number_with_delimiter(game.market_remain)%></span></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<center>
  <table width="90%" style="border: 3px solid black; font-size: 20px;">
    <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
      <th>Year</th>
      <th>Round</th>
      <th>Market Price</th>
      <th>Price</th>
      <th>PriceAdded</th>
      <th>Unit Sold</th>
      <th>Revenue</th>
    </tr>
    <%sales.each do |s|%>
    <tr align="right">
      <td><%=s.year%></td>
      <td><%=s.round%></td>
      <td><%=s.random_price%></td>
      <td><%=s.price%></td>
      <td><%=s.price_added%></td>
      <td><%=number_with_delimiter(s.quantity)%></td>
      <td><%=number_with_delimiter(s.cash)%></td>
    </tr>
    <%end%>
  </table>
  <div id="futures_area">
<%if game_setting.buyer_futures=='Y' || game_setting.seller_futures=='Y'%>
<%
  def cal_futures_value(contract_id)
    contract = Contract.find(contract_id)
    game = Game.find(contract.game_id)
    game_setting = game.game_setting
    if game.run_flag=='N'
      return contract.current_value
    else
      settlement_price = nil
      if game_setting.last_price
        settlement_price = game_setting.last_price
      else
        if game.current_year==1
          settlement_price = game_setting.f01_price.to_f
        else
          settlement_price = game_setting.average_price
        end
      end
      sold_price = nil
      if game.run_flag=='N'
        sold_price = (settlement_price * ((1.0 + game_setting.level1_debt_rate) ** (contract.end_year - game.current_year + 1))).round(2)
      else
        sold_price = (settlement_price * ((1.0 + game_setting.level1_debt_rate) ** (contract.end_year - game.current_year))).round(2)
      end
      val = nil
      if contract.c_type=='sell'
        val = (contract.unit_price - sold_price) * contract.total_unit
      else
        val = (sold_price - contract.unit_price) * contract.total_unit
      end
      #current_value = contract.initial_margin + val
      current_value = contract.current_value + val
      current_value = (current_value/10000.0).round * 10000
      return current_value
    end
  end

  f1 = 25.0
  if game.current_year==1
    f1 = game_setting.f01_price
  else
    f1 = game_setting.average_price * (1 + game_setting.level1_debt_rate)
  end
  f2 = f1 * (1 + game_setting.level1_debt_rate)
  f3 = f2 * (1 + game_setting.level1_debt_rate)
  f1 = f1.round(2)
  f2 = f2.round(2)
  f3 = f3.round(2)
%>
  <br/>
  <table style="border: 1px solid gray; width:90%">
    <tr align="center">
      <th></th>
      <th>สัญญา</th>
      <th>ส่งมอบปลายปีที่</th>
      <th>UnitPrice</th>
      <th>ราคาปัจจุบัน</th>
      <th>Value</th>
      <th></th>
    </tr>
    <%contracts = Contract.find(:all, :conditions=>["player_id=? AND open_flag=?", session[:player].id, 'Y'], :order=>"name, c_type")%>
    <%contracts.each do |c|%>
    <tr align="center">
      <td><%=c.name%></td>
      <td><%if c.c_type=="buy"%>ซื้อ<%else%>ขาย<%end%></td>
      <td><%=c.end_year%></td>
      <td><%=number_with_precision(c.unit_price, :precision=>2)%></td>
      <td>
        <%if game_setting.last_price%>
          <%=number_with_precision((game_setting.last_price * ((1.0 + game_setting.level1_debt_rate) ** (c.end_year - game.current_year))).round(2), :precision=>2)%>
        <%end%>
      </td>
      <td><%=number_with_delimiter(cal_futures_value(c.id))%></td>
      <td><a href="/running/close_futures2/<%=c.id%>" onclick="return confirm('ยืนยัน?')">ปิดสัญญา</a></td>
    </tr>
    <%end%>
  </table>
<%end%>
  </div>
</center>
<script type="text/javascript">
  function refresh_futures(){
    new Ajax.Updater('futures_area', '/game_runnings/futures_area/<%=player.id%>', {asynchronous:true, evalScripts:true});
  }
  <%if game_setting.buyer_futures=="Y" || game_setting.seller_futures=='Y'%>
    window.setInterval(refresh_futures,6000);
  <%end%>
</script>
